
const crypto = require('crypto')

const { createWebAPIRequest } = require('../../util/util')

module.exports = [
  {
    /**
    * @api {get} /music/login [邮箱登录]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 注意 : 此接口被网易和谐了 , 待修复 , 暂时使用手机登录
    * @apiParam {String} email 邮箱
    * @apiParam {String} password 密码
    * @apiParam {Boolean} [rememberLogin=true]  记住密码
    */
    url: '/music/login',
    request: (req, res) => {
      const email = req.query.email
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const md5sum = crypto.createHash('md5')
      md5sum.update(req.query.password)
      const data = {
        username: email,
        password: md5sum.digest('hex'),
        rememberLogin: true,
        clientToken:
      '1_jVUMqWEPke0/1/Vu56xCmJpo5vP1grjn_SOVVDzOc78w8OKLVZ2JH7IfkjSXqgfmh'
      }

      createWebAPIRequest(
        'music.163.com',
        '/weapi/login?csrf_token=',
        'POST',
        data,
        cookie,
        (musicReq, cookie) => {
          // console.log(musicReq)
          cookie = cookie && cookie.map(x => x.replace('Domain=.music.163.com', ''))
          res.set({
            'Set-Cookie': cookie
          })
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/login/cellphone [手机登录]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {String} phone 手机
    * @apiParam {String} password 密码
    * @apiParam {Boolean} [rememberLogin=true]  记住密码
    */
    url: '/music/login/cellphone',
    request: (req, res) => {
      const phone = req.query.phone
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const md5sum = crypto.createHash('md5')
      md5sum.update(req.query.password)
      const data = {
        phone: phone,
        password: md5sum.digest('hex'),
        rememberLogin: true
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/login/cellphone',
        'POST',
        data,
        cookie,
        (musicRes, cookies = []) => {
          const cookieStr =
        'appver=1.5.9;os=osx; channel=netease;osver=%E7%89%88%E6%9C%AC%2010.13.2%EF%BC%88%E7%89%88%E5%8F%B7%2017C88%EF%BC%89'
          cookieStr.split(';').forEach(item => {
            cookies.push(item + ';Path=/')
          })
          res.set({'Set-Cookie': cookies})
          res.send(musicRes)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/login/refresh [登录刷新]
    * @apiGroup Music
    * @apiPermission none
    */
    url: '/music/login/refresh',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/login/token/refresh`,
        'POST',
        data,
        cookie,
        (musicReq, cookie) => {
          res.set({
            'Set-Cookie': cookie
          })
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }
]
